<?php
class cls_control_productlist extends cls_control_base
{
	var $product_category; 
	var $product_child_list_pagesize=4;
	var $product_list_pagesize=8;
	var $current_page=1;
	
	function __construct()
	{
		$this->product_category=$_REQUEST['product_category'];
		$product_num=$_REQUEST['product_nume'];
		$this->current_page=$_REQUEST['current_page'];
	}
	
	function product_list_page()
	{		
		$category_serve=cls_serve_base::get_instance($this->name);
		$product_list=$category_serve->get_product_list($this->product_category);
		$productdetail_serve = cls_serve_base::get_instance('productdetail');
		for ($i=0;!empty($product_list[$i]);$i++)
		{
			$product_info=$product_list[$i];
			$product_id=$product_info['id'];
			$product_name=$product_info["name"];
			$product_author=$product_info["author"];
			$product_image=$product_info["image"];
			$product_price=$product_info["price"];
			$discount_info=$productdetail_serve->get_product_discount($product_id);
			$current_price=$category_serve->get_current_price($product_id);
			$product_list_page[$i]=array(
					"id"=>$product_id,
					"name"=>$product_name,
					"image"=>$product_image,
					"author"=>$product_author,
					"current_price"=>sprintf("%.2f",$productdetail_serve->get_current_price($product_id)),
					"price"=>sprintf("%.2f",$product_price),
					"discount_type"=>$discount_info["Type"]
					
					);
		}
		echo json_encode($product_list_page);
	}
	
	function parent_type_list()
	{
		$category_serve=cls_serve_base::get_instance($this->name);
		$category_child=$category_serve->get_category_child($this->product_category);
		$productdetail_serve = cls_serve_base::get_instance('productdetail');
		$parent_type_list=array();
		for($n=0;!empty($category_child[$n]);$n++)
		{		
		$product_list=$category_serve->get_product_list($category_child[$n]);
		for ($i=0;!empty($product_list[$i])&&$i<4;$i++)
		{
		$product_info=$product_list[$i];
		$product_id=$product_info['id'];
		$product_name=$product_info["name"];
		$product_author=$product_info["author"];
		$product_image=$product_info["image"];
		$product_price=$product_info["price"];
		$discount_info=$productdetail_serve->get_product_discount($product_id);
		$product_list_page[$i]=array(
						"id"=>$product_id,
						"name"=>$product_name,
						"image"=>$product_image,
						"author"=>$product_author,
						"current_price"=>sprintf("%.2f",$productdetail_serve->get_current_price($product_id)),
						"price"=>sprintf("%.2f",$product_price),
						"category_name"=>$category_serve->get_category_name($category_child[$n]),
						"category_id"=>$category_child[$n],
						"discount_type"=>$discount_info["Type"]				
				);
		}
		$parent_type_list=array_merge($parent_type_list,$product_list_page);
		}
		echo json_encode($parent_type_list);
	}
	
	/*function parent_type_list()
	{
		$category_serve=cls_serve_base::get_instance($this->name);
		$product_list=$category_serve->get_product_list($this->product_category);
		for ($i=0;!empty($product_list[$i])&&$i<4;$i++)
		{
		$product_info=$product_list[$i];
		$product_id=$product_info['id'];
		$product_name=$product_info["name"];
		$product_author=$product_info["author"];
		$product_image=$product_info["image"];
		$product_price=$product_info["price"];
		$current_price=$category_serve->get_current_price("$product_id");
		$product_list_page['product_list'][$i]=array(
				"id"=>$product_id,
				"name"=>$product_name,
				"image"=>$product_image,
				"author"=>$product_author,
				"current_price"=>sprintf("%.2f",$current_price),
				"price"=>sprintf("%.2f",$product_price)
									
		);
		}
		echo json_encode($product_list_page);
	}*/
	
	function get_category_level()
	{
		$category_serve=cls_serve_base::get_instance($this->name);
		$category_level=$category_serve->get_category_level($this->product_category);
		echo $category_level;
	}
	
	function get_first_child($product_category) //用于返回当前类的子类中的第一个
	{
		$category_serve=cls_serve_base::get_instance($this->name);
		$category_child=$category_serve->get_category_child($product_category);
		$category_child=$category_child[0];
		return $category_child;
	}		
	
	function get_product_list()
	{
		$category_serve=cls_serve_base::get_instance($this->name);
		$product_list=$category_serve->get_product_list($this->product_category);
		//p($product_list);
		return $product_list;
	}
	
	function show_product_list()
	{
		$category_serve=cls_serve_base::get_instance($this->name);
		$product_list=$category_serve->get_product_list($this->product_category);
		print_r($product_list);
		echo json_encode($product_list);
	}
	
	function show_parent_type_list()
	{
		$category_serve=cls_serve_base::get_instance($this->name);
		$product_list=$category_serve->get_parent_type_list($this->product_category);
	}
	
	/*function category_name()
	{
		$category_serve = cls_serve_base::get_instance($this->name);
		$category_info=$category_serve->get_category_name($this->product_category);
		echo json_encode($category_info);
	}*/
	
	function category_parent()
	{
		$category_serve = cls_serve_base::get_instance($this->name);
		$category_info=$category_serve->get_category_parent($this->product_category);
		return $category_info;
	}
	

	function category_original()
	{
		$category_serve = cls_serve_base::get_instance($this->name);
		$category_parent=$category_serve->get_category_parent($this->product_category);
		$category_original=$category_serve->get_category_parent($category_parent);
		return $category_original;
	}
	
	function category_brother()
	{
		$category_serve = cls_serve_base::get_instance($this->name);
		$category_parent=$category_serve->get_category_parent($this->product_category);
		$category_brother=$category_serve->get_category_child($category_parent);
		//$brother_list=array();
		for($i=0;!empty($category_brother[$i]);$i++)
		{
			$brother_list[$i]=array(
					"category_id"=>$category_brother[$i],
					"category_name"=>$category_serve->get_category_name($category_brother[$i])				
					);
		}
		echo json_encode($brother_list);
	}
	
	function show_category_relation()
	{
		$category_serve = cls_serve_base::get_instance($this->name);
		$category_relation=array(
				"current_category_id"=>$this->product_category,
				"current_category_name"=>$category_serve->get_category_name($this->product_category),
				"parent_category_id"=>$category_serve->get_category_parent($this->product_category),
				"parent_category_name"=>$category_serve->get_category_name($this->category_parent($this->product_category)),
				"original_category_id"=>$this->category_original(),
				"original_category_name"=>$category_serve->get_category_name($this->category_original()),
				);
		echo json_encode($category_relation);
	}
	
	function get_category_name()
	{
		$category_serve = cls_serve_base::get_instance($this->name);
		$category_name=$category_serve->get_category_name($this->product_category);
		echo $category_name;   //这里是用ajax方法拿到类名，所以没有转换成json
	}
	
	function get_parent_category()
	{
		$category_serve = cls_serve_base::get_instance($this->name);
		$parent_category=$category_serve->get_category_parent($this->product_category);
		$category_name=$category_serve->get_category_name($parent_category);
		$parent_info=array(
				"parent_id"=>$parent_category,				
				"parent_name"=>$category_name
				);
		echo json_encode($parent_info);
	}
	
	function get_original_category()
	{
		$category_serve = cls_serve_base::get_instance($this->name);
		$original_category=$category_serve->get_original_category();
		//p($original_category);
		return $original_category;
	}
	
	function my_original_category()
	{
		$category_serve = cls_serve_base::get_instance($this->name);
		$original_category=$this->get_original_category();
		for($i=0;!empty($original_category[$i]["id"]);$i++)
		{
			$first_child=$this->get_first_child($original_category[$i]["id"]);
			$my_original[$i]=array(
					"original_id"=>$original_category[$i]["id"],
					"original_name"=>$category_serve->get_category_name($original_category[$i]["id"]),
					"first_child_id"=>$first_child,
					"first_child_name"=>$category_serve->get_category_name($first_child)
					);
		}
		echo json_encode($my_original);
	}
	
	
}